﻿using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using DZTC.Base32Utility.Core;

namespace DZTC.Base32Utility
{
    public static class Program
    {
        private class Options
        {
            public ConversionDirection Direction { get; set; }
            public string InputFile { get; set; }
        }

        private static Options ParseOptions(string[] args)
        {
            var options = new Options();

            if (args.Length == 0) return null;

            if (args.Length < 2)
            {
                ShowUsage();
                Environment.Exit(1);
            }

            switch (args[0].ToLower())
            {
                case "--encode":
                    options.Direction = ConversionDirection.Encode;
                    break;

                case "--decode":
                    options.Direction = ConversionDirection.Decode;
                    break;

                default:
                    ShowUsage();
                    Environment.Exit(1);
                    break;
            }

            options.InputFile = args[1];

            return options;
        }

        private static void ShowUsage()
        {
            MessageBox.Show("base32 [--encode or --decode] [file]", "Usage");
        }

        private static string AddExtension(string fileName)
        {
            return fileName + Const.EXTENSION;
        }

        private static string RemoveExtension(string fileName)
        {
            var dir = Path.GetDirectoryName(fileName);
            var name = Path.GetFileNameWithoutExtension(fileName);

            if (String.IsNullOrEmpty(name)) return null;

            return Path.Combine(dir, name);
        }

        [STAThread]
        public static void Main(params  string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            OperationSettings settings = null;
            var options = ParseOptions(args);

            if (options == null)
            {
                settings = FilesForm.Show(new OperationSettings
                    {
                        Direction = ConversionDirection.Encode
                    });
            }
            else if (options.Direction == ConversionDirection.Encode)
            {
                var initialSettings = new OperationSettings
                    {
                        Direction = ConversionDirection.Encode,
                        InputFile = options.InputFile,
                        OutputFile = AddExtension(options.InputFile)
                    };

                settings = FilesForm.Show(initialSettings, showInputFile: false);
            }
            else if (options.Direction == ConversionDirection.Decode)
            {
                string outputFile = null;
                if (Path.GetExtension(options.InputFile) == Const.EXTENSION)
                {
                    outputFile = RemoveExtension(options.InputFile);
                }

                var initialSettings = new OperationSettings
                    {
                        Direction = ConversionDirection.Decode,
                        InputFile = options.InputFile,
                        OutputFile = outputFile
                    };

                settings = FilesForm.Show(initialSettings, showInputFile: false);
            }

            if (settings == null) return;

            ConversionForm.Run(settings.InputFile, settings.OutputFile, settings.Direction);
        }
    }
}